#!/bin/bash

git add .
git commit -m "deploy"
git push

# 远程服务器信息
REMOTE_HOST="121.89.84.104"
REMOTE_USER="root"
REMOTE_DIR="/var/www/html6"
LOCAL_DIST_DIR="./dist"

# 1. 在本地运行 npm build
echo "正在本地构建项目..."
npm run build
if [ $? -ne 0 ]; then
    echo "构建失败，请检查错误！"
    exit 1
fi
echo "本地构建完成！"

# 2. 删除远程服务器目录内容
echo "正在清理远程服务器目录..."
ssh ${REMOTE_USER}@${REMOTE_HOST} "rm -rf ${REMOTE_DIR}/*"
if [ $? -ne 0 ]; then
    echo "远程清理失败！"
    exit 1
fi
echo "远程目录清理完成！"

# 3. 上传构建文件
echo "正在上传文件到远程服务器..."
scp -r ${LOCAL_DIST_DIR}/* ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/
if [ $? -ne 0 ]; then
    echo "文件上传失败！"
    exit 1
fi
echo "文件上传成功！"

echo "部署完成！"